import { getTree as getDeptTree, getCompanyTree } from '@/api/department'
/**
 * 缓存基础数据
 */
const state = {
  deptTree: [],
  companyTree: []
}

const mutations = {
  SET_DEPTTREE: (state, deptTree) => {
    state.deptTree = deptTree
  },
  SET_COMPANYTREE: (state, companyTree) => {
    state.companyTree = companyTree
  }
}

const actions = {
  setDeptTree({ commit }) {
    return new Promise((resolve, reject) => {
      getDeptTree().then(response => {
        commit('SET_DEPTTREE', response)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  setCompanyTree({ commit }) {
    return new Promise((resolve, reject) => {
      getCompanyTree().then(response => {
        commit('SET_COMPANYTREE', response)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  }

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
